<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="global.jsp"></jsp:include>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>科研管理平台 - 首页</title>
	<script type="text/javascript">
		function init(){
			// 实例化树菜单
			$("#tree").tree({
				url:'initMenu',
				method:'get',
				animate: true,
				lines:true,
				formatter:function(node){
					//return "<span style='font-size: 14px;'>" + node.text + "</span>";
					return node.text;
				},
				onClick:function(node){
					if(node.attributes){
						//openTab(node.text, node.attributes.url);
						//addTab(node.text, node.attributes.url, '');
						addTab(node.text, node.attributes.url);
					}
				}
			});
		}
		$(function(){
			var pwdValue = "${currentUser.password}";
			var checkNum = /^.*\d+.*$/;
			var checkWork = /^.*[A-Za-z]+.*$/;
			if(!checkWork.test(pwdValue) || !checkNum.test(pwdValue) || pwdValue.length < 8){
				$('#editPWDwindow').window('open');
			}else{
				init();
				addTab("首页", "awaitAdvices");
			}
			
			/*
			// 新增Tab
			function openTab(text,url){
				if($("#tabs").tabs('exists',text)){
					if(text == '首页'){
						$("#tabs").tabs('select',text);
						return;
					}else{
						$("#tabs").tabs('close',text);
					}
				}else{
					var content="<iframe frameborder='0' scrolling='auto' style='width:100%;height:100%' src="+url+"></iframe>";
					$("#tabs").tabs('add',{
						title:text,
						closable:true,
						content:content
					});
				}
			}
			*/
			/*
			function addTab(title, href, icon) {
				var tt = $('#tabs');
				if (tt.tabs('exists', title)) {
					tt.tabs('select', title);
					var tab = $('#tabs').tabs('getSelected');
					if (tab) {
						var index = $('#tabs').tabs('getTabIndex', tab);
						$('#tabs').tabs('close', index);
					}
				}
				//var content = "";
				if (href) {
					tt.tabs('add', {
						title : title,
						closable : true,
						href : href,
						iconCls : icon || 'icon-default'
					});
				} else {
					tt.tabs('add', {
						title : title,
						closable : true,
						//content : "请输入请求地址!",
						iconCls : icon || 'icon-default'
					});
				}
			}
			*/
			
			tabCloseEven();

			$('.cs-navi-tab').click(function() {
				var $this = $(this);
				var href = $this.attr('src');
				var title = $this.text();
				addTab(title, href);
			});

			var themes = {
				'gray' : 'themes/gray/easyui.css',
				'black' : 'themes/black/easyui.css',
				'bootstrap' : 'themes/bootstrap/easyui.css',
				'default' : 'themes/default/easyui.css',
				'metro' : 'themes/metro/easyui.css'
			};

			var skins = $('.li-skinitem span').click(function() {
				var $this = $(this);
				if($this.hasClass('cs-skin-on')) return;
				skins.removeClass('cs-skin-on');
				$this.addClass('cs-skin-on');
				var skin = $this.attr('rel');
				$('#swicth-style').attr('href', themes[skin]);
				setCookie('cs-skin', skin);
				skin == 'dark-hive' ? $('.cs-north-logo').css('color', '#FFFFFF') : $('.cs-north-logo').css('color', '#000000');
			});

			if(getCookie('cs-skin')) {
				var skin = getCookie('cs-skin');
				$('#swicth-style').attr('href', themes[skin]);
				$this = $('.li-skinitem span[rel='+skin+']');
				$this.addClass('cs-skin-on');
				skin == 'dark-hive' ? $('.cs-north-logo').css('color', '#FFFFFF') : $('.cs-north-logo').css('color', '#000000');
			}
			
			$("#editPWDForm").form({
				onSubmit: function(){
					var isValid = $(this).form('validate');
					if(isValid){
						var pwd = $('#pwd').val();
						var checkNum = /^.*\d+.*$/;
						var checkWork = /^.*[A-Za-z]+.*$/;
						if(!checkWork.test(pwd) || !checkNum.test(pwd)){
							isValid = false;
							$("#pwdError").text("密码必须是字母和数字组合.");
						}else if(pwd != $("#rpwd").val()){
							isValid = false;
							$("#pwdError").text("两次密码不一致.");
						}
					}
					return isValid;	// 返回false将停止form提交 
			    },
				success:function(data){
					var val = $.parseJSON(data);
					if(val.success){
						bottomRight('提示', '密码修改成功！');
						$('#editPWDwindow').window('close');
						init();
						addTab("首页", "awaitAdvices");
					}else{
						$.messager.alert("提示", val.msg, "error");
					}
				}
			});
			/*
			$.extend($.fn.validatebox.defaults.rules, {
			    minLength: {
			        validator: function(value, param){    
			            return value.length >= param[0];    
			        },
			        message: '至少 {0}个字符.';
			    }    
			});
			*/
		});

		function setCookie(name,value) {//两个参数，一个是cookie的名子，一个是值
		    var Days = 0.01; //此 cookie 将被保存 0.01 天
		    var exp = new Date();    //new Date("December 31, 9998");
		    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
		    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
		}

		function getCookie(name) {//取cookies函数 
		    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
		     if(arr != null) return unescape(arr[2]); return null;
		}
		
		function addTab(title, url){
			if ($('#tabs').tabs('exists', title)){
				$('#tabs').tabs('select', title);//选中并刷新
				var currTab = $('#tabs').tabs('getSelected');
				var url = $(currTab.panel('options').content).attr('src');
				if(url != undefined && currTab.panel('options').title != 'Home') {
					$('#tabs').tabs('update',{
						tab:currTab,
						options:{
							content:createFrame(url)
						}
					})
				}
			} else {
				var content = createFrame(url);
				$('#tabs').tabs('add',{
					title:title,
					content:content,
					closable:true
				});
			}
			tabClose();
		}
		function createFrame(url) {
			var s = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
			return s;
		}
				
		function tabClose() {
			//双击关闭TAB选项卡
			$(".tabs-inner").dblclick(function(){
				var subtitle = $(this).children(".tabs-closable").text();
				$('#tabs').tabs('close',subtitle);
			});
			//为选项卡绑定右键
			$(".tabs-inner").bind('contextmenu',function(e){
				$('#mm').menu('show', {
					left: e.pageX,
					top: e.pageY
				});

				var subtitle =$(this).children(".tabs-closable").text();

				$('#mm').data("currtab",subtitle);
				$('#tabs').tabs('select',subtitle);
				return false;
			});
		}		
		//绑定右键菜单事件
		function tabCloseEven() {
			//刷新
			$('#mm-tabupdate').click(function(){
				var currTab = $('#tabs').tabs('getSelected');
				var url = $(currTab.panel('options').content).attr('src');
				if(url != undefined) {
					$('#tabs').tabs('update',{
						tab:currTab,
						options:{
							content:createFrame(url)
						}
					})
				}
			})
			//关闭当前
			$('#mm-tabclose').click(function(){
				var currtab_title = $('#mm').data("currtab");
				$('#tabs').tabs('close', currtab_title);
			})
			//全部关闭
			$('#mm-tabcloseall').click(function(){
				$('.tabs-inner span').each(function(i,n){
					var t = $(n).text();
					$('#tabs').tabs('close',t);
				});
				addTab("首页", "awaitAdvices");
			});
			//关闭除当前之外的TAB
			$('#mm-tabcloseother').click(function(){
				var prevall = $('.tabs-selected').prevAll();
				var nextall = $('.tabs-selected').nextAll();		
				if(prevall.length>0){
					prevall.each(function(i,n){
						var t=$('a:eq(0) span',$(n)).text();
						$('#tabs').tabs('close',t);
					});
				}
				if(nextall.length>0) {
					nextall.each(function(i,n){
						var t=$('a:eq(0) span',$(n)).text();
						$('#tabs').tabs('close',t);
					});
				}
				return false;
			});
			//关闭当前右侧的TAB
			$('#mm-tabcloseright').click(function(){
				var nextall = $('.tabs-selected').nextAll();
				if(nextall.length==0){
					//msgShow('系统提示','后边没有啦~~','error');
					alert('后边没有啦~~');
					return false;
				}
				nextall.each(function(i,n){
					var t=$('a:eq(0) span',$(n)).text();
					$('#tabs').tabs('close',t);
				});
				return false;
			});
			//关闭当前左侧的TAB
			$('#mm-tabcloseleft').click(function(){
				var prevall = $('.tabs-selected').prevAll();
				if(prevall.length==0){
					alert('到头了，前边没有啦~~');
					return false;
				}
				prevall.each(function(i,n){
					var t=$('a:eq(0) span',$(n)).text();
					$('#tabs').tabs('close',t);
				});
				return false;
			});

			//退出
			$("#mm-exit").click(function(){
				$('#mm').menu('hide');
			})
		}
	</script>
</head>
<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height:60px; background: url('/images/notice.jpg') #A5DBFC; padding:10px; background-size: cover; overflow: hidden;">
		<marquee style="color:green; font-size:18px;">科研项目管控平台</marquee>
		<p style="float: left; color: red;">本系统为非涉密系统，在使用过程中请将涉密信息使用特殊字符代替。</p>
		<p style="text-align: right; margin-right: 50px;">当前用户：${currentUser.name }<a href="loginout">退出</a></p>
		<div id="editPWDwindow" class="easyui-window" title="密码强度不足" data-options="modal:true,closed:true,iconCls:'icon-edit',minimizable:false, maximizable: false, closable:false" style="width:400px; height:210px;padding:5px;">
			<div class="easyui-layout" data-options="fit:true">
				<div data-options="region:'center'" style="padding:10px;">
					<form id="editPWDForm" action="changePwd" method="POST" style="width: 100%; height: 100%;">
						<input type="hidden" name="id" value="${currentUser.id }" />
						<table width="100%" height="100%">
							<tr>
								<td width="100px;" align="right">新密码：</td>
								<td>
									<input id="pwd" type="password" class="easyui-validatebox" data-options="validType:'length[8, 50]', required:true" style="width: 200px;" /> 
								</td>
							</tr>
							<tr>
								<td width="100px;" align="right">再次输入新密码：</td>
								<td>
									<input id="rpwd" name="password" type="password" class="easyui-validatebox" required="required" style="width: 200px;" />  
								</td>
							</tr>
							<tr>
								<td colspan="2" align="center">
									<span id="pwdError" style="color: red;"> </span>  
								</td>
							</tr>
						</table>
					</form>
				</div>
				<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
					<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="javascript:$('#editPWDForm').submit()" style="width:80px">修 改</a>
				</div>
			</div>
		</div>
	</div>
	<div data-options="region:'west',split:true,title:'菜单信息'" style="width:172px;padding:2px;">
		<div class="easyui-accordion" data-options="fit:true,border:false," style="overflow: auto;">
			<ul class="easyui-tree" id="tree"></ul>
		</div>
	</div>
	<!-- 
	<div data-options="region:'east',split:true,collapsed:true,title:'系统消息'" style="width:172px;padding:2px;">
		<ul class="easyui-tree" data-options="url:'initMenu',method:'get',animate:true,dnd:true"></ul>
	</div>
	 -->
	<div region="center">
		<div class="easyui-tabs" fit="true" border="false" id="tabs">
			<!-- 
			<div title="首页" >
				<div align="center"><font color="red" size="8">请使用IE9,谷歌，火狐，不支持IE低版本</font></div>
			</div>
			-->
		</div>
	</div>
	<div data-options="region:'south',border:false" style="height:25px;background:#CDE8FA;padding:10px; text-align: center;">
		<font size="6"><span style="color: blue; ">@</span>中国航天科工二院七〇六所</font>
	</div>
	
	<div id="mm" class="easyui-menu cs-tab-menu">
		<div id="mm-tabupdate">刷新</div>
		<div class="menu-sep"></div>
		<div id="mm-tabclose">关闭</div>
		<div id="mm-tabcloseother">关闭其他</div>
		<div id="mm-tabcloseall">关闭全部</div>
	</div>
</body>
</html>